﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace GrapeCity.Windows.Testing.Tools
{
    public static class WrapHelper
    {
        #region Convert a array to a wrapped array.
        public static OT[] ArrayConvertToWrapper<OT, IT>(IT[] iArray)
            where OT : IWrapper, new()
            where IT : class
        {
            OT[] oArray = default(OT[]);
            if (iArray == null)
            {
                return oArray;
            }
            else
            {
                int count = iArray.Length;
                oArray = new OT[count];
                for (int i = 0; i < count; i++)
                {
                    oArray[i] = new OT();
                    oArray[i].WrapInitialize(iArray[i]);
                }
            }
            return oArray;
        }

        public static OT[,] ArrayConvertToWrapper<OT, IT>(IT[,] iArray)
            where OT : IWrapper, new()
            where IT : class
        {
            OT[,] oArray = default(OT[,]);
            if (iArray == null)
            {
                return oArray;
            }
            else
            {
                int lower0 = iArray.GetLowerBound(0);
                int count0 = iArray.GetUpperBound(0) + 1;
                int lower1 = iArray.GetLowerBound(1);
                int count1 = iArray.GetUpperBound(1) + 1;
                oArray = new OT[count0, count1];
                for (int i = lower0; i < count0; i++)
                {
                    for (int j = lower1; j < count1; j++)
                    {
                        oArray[i, j] = new OT();
                        oArray[i, j].WrapInitialize(iArray[i, j]);
                    }
                }
            }
            return oArray;
        }
        #endregion

        #region Convert a array from a wrapped array.
        public static OT[] ArrayConvertFromWrapper<OT, IT>(IT[] iArray)
            where OT : class
            where IT : IWrapper
        {
            OT[] oArray = default(OT[]);
            if (iArray == null)
            {
                return oArray;
            }
            else
            {
                int count = iArray.Length;
                oArray = new OT[count];
                for (int i = 0; i < count; i++)
                {
                    oArray[i] = (OT)iArray[i].GetOwnerInWrapper();
                }
            }
            return oArray;
        }

        public static OT[,] ArrayConvertFromWrapper<OT, IT>(IT[,] iArray)
            where OT : class
            where IT : IWrapper
        {
            OT[,] oArray = default(OT[,]);
            if (iArray == null)
            {
                return oArray;
            }
            else
            {
                int lower0 = iArray.GetLowerBound(0);
                int count0 = iArray.GetUpperBound(0) + 1;
                int lower1 = iArray.GetLowerBound(1);
                int count1 = iArray.GetUpperBound(1) + 1;
                oArray = new OT[count0, count1];
                for (int i = lower0; i < count0; i++)
                {
                    for (int j = lower1; j < count1; j++)
                    {
                        oArray[i, j] = (OT)iArray[i, j].GetOwnerInWrapper();
                    }
                }
            }
            return oArray;
        }
        #endregion
    }

    public interface IWrapper
    {
        object GetOwnerInWrapper();

        void WrapInitialize(object obj);
    }
}
